//
//  NewFeatureCollectionViewController.swift
//  微博三天
//
//  Created by jessica on 15/11/13.
//  Copyright © 2015年 jessica. All rights reserved.
//


//新特性界面
import UIKit

//方法提供了很多
let imageCount = 4

private let reuseIdentifier = "Cell"

class NewFeatureCollectionViewController: UICollectionViewController {
    
    
    //先构造
    init(){
        //布局
        let layout = UICollectionViewFlowLayout()
        
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = .Horizontal //水平滚动
        super.init(collectionViewLayout:layout)//添加
        
        //设置分页滚动
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false//弹簧效果
        collectionView?.showsHorizontalScrollIndicator = false//水平
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //
    //
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 下面定义了 UICollectionViewCell -> NewFeatureCell
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        
    }
    
    //删除两个暂时用不上的
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return imageCount
        
    }
    //
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)as! NewFeatureCell
        
        //这里要 UITabelView就是row , 这里是 item
        cell.index = indexPath.item//这个是赋值
        
        return cell
    }
    
    //动画要添加在这里
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        
        let page = Int (scrollView.contentOffset.x / screenW)
        
        if  page == imageCount - 1 {
            if  let cell = collectionView?.visibleCells().last as?NewFeatureCell{
            
              cell.startAnimation()
                
            }
            
            
        }
        
        
    }
    
    
}

//NewFeatureCell

class NewFeatureCell: UICollectionViewCell {
    
    
    var index : Int = 0 {
        
        didSet {
            
            iconImage.image = UIImage(named: "new_feature_\(index + 1)")
            
        }
        
    }
    
    //开始动画的效果
    private func startAnimation() {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
                                                          //弹力系数
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5,
            //重力
            initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            
            
            //后来
            self.startButton.hidden = true                    //按比例缩放
            self.startButton.transform = CGAffineTransformMakeScale(1, 1)
            //加的
            
            }) { (_) -> Void in
                
            print("OK")
        }
    }

    
    //
    //构造
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        //添加控件及约束  /snp_edges四边为0 紧贴着
        contentView.addSubview(iconImage)
        
        contentView.addSubview(startButton)
        
        
        iconImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        //开始按钮的约束
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
            make.centerX.equalTo(contentView.snp_centerX)

    }
        
        //设置按钮点击事件
        
        startButton.addTarget(self, action: "startVN", forControlEvents: .TouchUpInside)
        startButton.hidden = true
        
        
}
    
    @objc private func startVN(){
    
        //  按钮出现的时候发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
    }
    //
    //懒加载添加控件
    
    private lazy var iconImage : UIImageView = UIImageView()
    
    private lazy var startButton : UIButton = {
        
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        btn.setTitle("越南国度", forState: .Normal)
        
        //
//        btn.hidden = true //隐藏
        return btn
        
        
        }()
    
    
}

